package com.quan.elastic.job.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.quan.elastic.job.handler.ElasticJobListener;

@Configuration
@ConfigurationProperties(prefix = "elasticjob.zookeeper")
public class ElasticJobConfiguration {

	private String serverlists;

	private String namespace;

	@Autowired
	private DruidDataSource dataSource;

	@Bean
	public CoordinatorRegistryCenter createRegistryCenter() {
		CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(
				new ZookeeperConfiguration(serverlists, namespace));
		regCenter.init();
		return regCenter;
	}

	@Bean
	public ElasticJobListener elasticJobListener() {
		return new ElasticJobListener(100, 100);
	}

	/**
	 * 将作业运行的痕迹进行持久化到DB
	 * Elastic Job 会自动创建 job_execution_log 和 job_status_trace_log 表
	 * @return
	 */
	@Bean
	public JobEventConfiguration jobEventConfiguration() {
		return new JobEventRdbConfiguration(dataSource);
	}

	public String getServerlists() {
		return serverlists;
	}

	public void setServerlists(String serverlists) {
		this.serverlists = serverlists;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}
}
